/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Markup;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "FilterBy = {DebuggerDisplayString}" )]
  [ContentProperty( "ChildExpressions" )]
  public abstract class FilterExpressionOperator : FilterExpressionBase
  {
    #region Constructors

    internal FilterExpressionOperator( params FilterExpressionBase[] expressions )
      : this( ( IEnumerable<FilterExpressionBase> )expressions )
    {
    }

    internal FilterExpressionOperator( IEnumerable<FilterExpressionBase> expressions )
      : this()
    {
      if( expressions != null )
      {
        foreach( FilterExpressionBase filterExpression in expressions )
        {
          m_childExpressions.InternalAdd( filterExpression );
        }
      }
    }

    internal FilterExpressionOperator()
    {
      m_childExpressions = new FilterExpressionCollection( this );
    }

    #endregion

    #region ChildExpressions Property

    public FilterExpressionCollection ChildExpressions
    {
      get
      {
        return m_childExpressions;
      }
    }

    private FilterExpressionCollection m_childExpressions; //null

    #endregion

    public override int GetHashCode()
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      return this.GetType().GetHashCode();
    }

    public override bool Equals( object obj )
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      if( object.ReferenceEquals( this, obj ) )
        return true;

      FilterExpressionOperator expression = obj as FilterExpressionOperator;
      if( expression == null )
        return false;

      if( expression.GetHashCode() != this.GetHashCode() )
        return false;

      if( expression.ChildExpressions.Count != this.ChildExpressions.Count )
        return false;

      return this.ChildExpressions.SequenceEqual( expression.ChildExpressions );
    }

    public void Add( FilterExpressionBase expression )
    {
      if( this.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify a FilterExpressionOperator that has been frozen" );

      if( expression != null )
      {
        Debug.Assert(
          !object.ReferenceEquals( expression, this ),
          "Cannot add self." );

        if( !m_childExpressions.Contains( expression ) )
        {
          m_childExpressions.InternalAdd( expression );
        }
      }
    }

    internal override void FreezeOverride()
    {
      if( m_childExpressions.Count < 2 )
        throw new InvalidOperationException( this.GetType().Name + " requires at least two child expressions to be valid." );

      foreach( FilterExpressionBase child in m_childExpressions )
      {
        child.Freeze();
      }

      base.FreezeOverride();
    }
  }
}
